package com.fedex;

import com.fedex.country.stub.*;

//

/**
 * Sample code to call the FedEx Postal code inquiry Web Service
 * <p/>
 * com.fedex.pmis.stub is generated via WSDL2Java, like this:<br>
 * <pre>
 * java org.apache.axis.wsdl.WSDL2Java -w -p com.fedex.csppmis.stub http://www.fedex.com/...../PackageMovementInformationService?wsdl
 * </pre>
 * <p/>
 * This sample code has been tested with JDK 5 and Apache Axis 1.4
 */
public class PostalCodeInquiryWebServiceClient {
    //
    public static void main(String[] args) {
        //
        PostalCodeInquiryRequest request = new PostalCodeInquiryRequest();
        ClientDetail clientDetail = createClientDetail();
        request.setClientDetail(clientDetail);
        request.setWebAuthenticationDetail(createWebAuthenticationDetail());

        //
        VersionId versionId = new VersionId("cnty", 2, 0, 0);
        request.setVersion(versionId);
        //
        TransactionDetail transactionDetail = new TransactionDetail();
        transactionDetail.setCustomerTransactionId("java sample - PostalCodeInquiryRequest"); //This is decode reference field for the customer.  Any value can be used and will be provided in the return.
        request.setTransactionDetail(transactionDetail);
        //
        request.setPostalCode("32810");
        request.setCountryCode("US");

        //
        try {
            // Initialize the service
            CountryServiceLocator service;
            CountryPortType port;
            //
            service = new CountryServiceLocator();
            updateEndPoint(service);
            port = service.getCountryServicePort();
            // This is the call to the web service
            PostalCodeInquiryReply reply = port.postalCodeInquiry(request);
            //
            if (isResponseOk(reply.getHighestSeverity())) {
                System.out.println("--- Express Description ---");
                System.out.println("Location id - " + reply.getExpressDescription().getLocationId());
                System.out.println("Postal Code - " + reply.getExpressDescription().getPostalCode());
                System.out.println("Service area - " + reply.getExpressDescription().getServiceArea());
                System.out.println("--- Express Freight Description ---");
                System.out.println("Location id - " + reply.getExpressFreightDescription().getLocationId());
                System.out.println("Postal Code - " + reply.getExpressFreightDescription().getPostalCode());
                System.out.println("Service area - " + reply.getExpressFreightDescription().getServiceArea());
            }
            System.out.println("");
            printNotifications(reply.getNotifications());

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    private static boolean isResponseOk(NotificationSeverityType notificationSeverityType) {
        if (notificationSeverityType == null) {
            return false;
        }
        if (notificationSeverityType.equals(NotificationSeverityType.WARNING) ||
                notificationSeverityType.equals(NotificationSeverityType.NOTE) ||
                notificationSeverityType.equals(NotificationSeverityType.SUCCESS)) {
            return true;
        }
        return false;
    }


    private static ClientDetail createClientDetail() {
        ClientDetail clientDetail = new ClientDetail();
        String accountNumber = System.getProperty("accountNumber");
        String meterNumber = System.getProperty("meterNumber");

        //
        // See if the accountNumber and meterNumber properties are set,
        // if set use those values, otherwise default them to "XXX"
        //
        if (accountNumber == null) {
            accountNumber = "XXX"; // Replace "XXX" with clients account number
        }
        if (meterNumber == null) {
            meterNumber = "XXX"; // Replace "XXX" with clients meter number
        }
        clientDetail.setAccountNumber(accountNumber);
        clientDetail.setMeterNumber(meterNumber);
        return clientDetail;
    }

    private static WebAuthenticationDetail createWebAuthenticationDetail() {
        WebAuthenticationCredential wac = new WebAuthenticationCredential();
        String key = System.getProperty("key");
        String password = System.getProperty("password");

        //
        // See if the key and password properties are set,
        // if set use those values, otherwise default them to "XXX"
        //
        if (key == null) {
            key = "XXX"; // Replace "XXX" with clients key
        }
        if (password == null) {
            password = "XXX"; // Replace "XXX" with clients password
        }

        wac.setKey(key);
        wac.setPassword(password);

        return new WebAuthenticationDetail(wac);
    }

    private static void printNotifications(Notification[] notifications) {
        System.out.println("Notifications:");
        if (notifications == null || notifications.length == 0) {
            System.out.println("  No notifications returned");
        }
        for (int i = 0; i < notifications.length; i++) {
            Notification n = notifications[i];
            System.out.print("  Notification no. " + i + ": ");
            if (n == null) {
                System.out.println("null");
                continue;
            } else {
                System.out.println("");
            }
            NotificationSeverityType nst = n.getSeverity();

            System.out.println("    Severity: " + (nst == null ? "null" : nst.getValue()));
            System.out.println("    Code: " + n.getCode());
            System.out.println("    Message: " + n.getMessage());
            System.out.println("    Source: " + n.getSource());
        }
    }

    private static void updateEndPoint(CountryServiceLocator serviceLocator) {
        String endPoint = System.getProperty("endPoint");
        if (endPoint != null) {
            serviceLocator.setCountryServicePortEndpointAddress(endPoint);
        }
    }
}
